package com.llsydn.demo.animation2;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2018/10/10 14:13
 */
public class AnimationSecondActivity extends AppCompatActivity {
    private ImageView iv_main_scan;
    private TextView tv_main_scan;
    private ProgressBar pb_main_scan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_second);

        iv_main_scan = findViewById(R.id.iv_main_scan);
        tv_main_scan = findViewById(R.id.tv_main_scan);
        pb_main_scan = findViewById(R.id.pb_main_scan);

        //1.显示扫描动画
        showScanAnimation();

        //2.扫描，并显示扫描进度
        scan();
    }

    /**
     * 显示扫描动画
     * imageview的旋转动画
     */
    public void showScanAnimation() {
        //创建rotate对象
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置rotate对象的参数
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        //启动动画
        iv_main_scan.startAnimation(rotateAnimation);
    }

    /**
     * 扫描，并显示扫描进度
     */
    public void scan() {
        //启动异步任务
        new AsyncTask<Void, Integer, Void>() {
            //1.主线程显示提示视图
            @Override
            protected void onPreExecute() {
                tv_main_scan.setText("手机杀毒引擎正在扫描中...");
            }

            //2.分线程，做长时间的工作(扫描应用)
            @Override
            protected Void doInBackground(Void... voids) {
                int appCount = 60;
                //设置进度条的最大值
                pb_main_scan.setMax(appCount);
                for (int i = 0; i < appCount; i++) {
                    SystemClock.sleep(40);
                    //扫描完成一个
                    //发布进度
                    publishProgress();
                }
                return null;
            }

            //3.主线程更新界面
            @Override
            protected void onPostExecute(Void aVoid) {
                pb_main_scan.setVisibility(View.GONE);
                tv_main_scan.setText("扫描完成，未发现病毒应用，请放心使用！");
                iv_main_scan.clearAnimation();
            }

            //在主线程执行，更新进度
            @Override
            protected void onProgressUpdate(Integer... values) {
                pb_main_scan.incrementProgressBy(1);
                //pb_main_scan.setProgress(pb_main_scan.getProgress()+1);
            }
        }.execute();

    }
}
